package com.gok.seasonmovie.controller;

import com.gok.seasonmovie.entity.Seat;
import com.gok.seasonmovie.entity.UserDTO;
import com.gok.seasonmovie.service.WeChatService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping()
public class WeChatController {

    @Resource
    private WeChatService weChatService;

    @PostMapping("/wechat/login")
    public Map<String, Object> login(@RequestParam String code) {
        try {
            return weChatService.login(code);
        } catch (RuntimeException e) {
            // 这里捕获了MyBatis可能抛出的运行时异常
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 600);
            errorResponse.put("msg", "Database error: " + e.getMessage());
            return errorResponse;
        }
    }

    @PostMapping("api/user/info")
    public Map<String, Object> uploadUserInfo(@RequestBody UserDTO userInfoDTO) {
        try{
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "ok");
            weChatService.saveUserInfo(userInfoDTO);
            return response;
        }catch (RuntimeException e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 600);
            errorResponse.put("msg", "Database error: " + e.getMessage());
            return errorResponse;
        }
    }


}